﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

namespace OneByOneSolitaire
{
    public class Ball
    {
        private int m_x, m_y;
        private int m_lastX, m_lastY;
        private MainPage m_board;
        private Canvas m_boardCanvas;
        private ImageSource m_image;
        private Rectangle m_frame;
        private Image m_imageCtrl;
        private int m_offsetX;
        private int m_offsetY;
        private bool m_enabled;
        
        public int x { get { return m_x; } set { m_x = value; } }
        public int y { get { return m_y; } set { m_y = value; } }
        public int lastX { get { return m_lastX; } set { m_lastX = value; } }
        public int lastY { get { return m_lastY; } set { m_lastY = value; } }
        public bool enabled { get { return m_enabled; } set { m_enabled = value; } }
        public Image imageCtrl { get { return m_imageCtrl; } }

        public Rectangle position 
        { 
            get { return m_frame; } 
            set 
            { 
                m_frame = value;
                m_imageCtrl.Height = value.height;
                m_imageCtrl.Width = value.width;
                Canvas.SetLeft(m_imageCtrl, value.x);
                Canvas.SetTop(m_imageCtrl, value.y);
                m_lastX = value.x;
                m_lastY = value.y;
            } 
        }

        public Rectangle frame
        {
            get { return m_frame; }
            set
            {
                m_frame = value;
                m_imageCtrl.Height = value.height;
                m_imageCtrl.Width = value.width;
                m_lastX = value.x;
                m_lastY = value.y;
            }
        }

        public Ball(MainPage board, ImageSource image, Canvas boardCanvas) 
        {
            m_enabled = true;
            m_board = board;
            m_boardCanvas = boardCanvas;
            m_image = image;
            m_imageCtrl = new Image();
            m_imageCtrl.Stretch = Stretch.None;
            m_imageCtrl.Source = image;
            m_imageCtrl.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseLeftButtonDown);
            m_imageCtrl.MouseLeftButtonUp += new MouseButtonEventHandler(OnMouseLeave);
            m_imageCtrl.MouseMove += new MouseEventHandler(OnMouseLeftButtonUp);
            m_boardCanvas.Children.Add(m_imageCtrl);
        } 

        public void setImage(ImageSource image) {
            m_imageCtrl.Source = image;
        }

        public void restoreToInitialPosition() {
            m_board.setBallOriginalPosition(this);
        }

        private void OnMouseLeftButtonDown(object sender, MouseEventArgs e)
        {
            m_board.startMovingBall(this);
            Point point = e.GetPosition(m_boardCanvas);
            m_offsetX = m_lastX - (int)point.X;
            m_offsetY = m_lastY - (int)point.Y;
        }

        private void OnMouseLeftButtonUp(object sender, MouseEventArgs e)
        {
            if (m_enabled)
            {
                Point point = e.GetPosition(m_boardCanvas);
                Canvas.SetLeft(m_imageCtrl, (int)point.X + m_offsetX);
                Canvas.SetTop(m_imageCtrl, (int)point.Y + m_offsetY);
            }
        }

        private void OnMouseLeave(object sender, MouseEventArgs e)
        {
            if (m_enabled)
            {
                m_board.endMovingBall();
                if (!m_board.move(this, e.GetPosition(m_boardCanvas))) 
                    restoreToInitialPosition();
            }
        }

    }
}
